﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Marmat.Forms.Design
{
    public partial class BindingEditorForm : Form
    {
        private Control _control;
        private readonly DataTable data;
        private Marmat.Forms.BindingField _bindingField;
        public BindingEditorForm(System.Windows.Forms.Control control, Marmat.Forms.BindingField bindingField)
        {
            InitializeComponent();
            this._control = control;
            this._bindingField = bindingField;

            data = new DataTable();
            data.Columns.Add("Name");
            data.Columns.Add("Tag");
            this.TreeList2.DataSource = data;
        }

        private void BindingEditorForm_Load(object sender, EventArgs e)
        {
            foreach (IComponent component in this._control.FindForm().Container.Components)
            {
                if (component is IBindingSource source)
                {
                    DevExpress.XtraTreeList.Nodes.TreeListNode node = this.TreeList1.Nodes.Add();
                    node.Tag = source;
                }
            }
            if (this.TreeList1.Nodes.Count > 0)
            {
                this.TreeList1.FocusedNode = this.TreeList1.Nodes[0];
                TreeList1_FocusedNodeChanged(TreeList1, new DevExpress.XtraTreeList.FocusedNodeChangedEventArgs(null, this.TreeList1.FocusedNode));
            }
        }

        private void TreeList1_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            if (e.Node.Tag is IBindingSource source)
            {
                this.data.Rows.Clear();
                this.TreeList2.Nodes.Clear();
                foreach (var dataMember in source.GetColumnCollection())
                {
                    DataRow row = data.NewRow();
                    row["Name"] = dataMember.Name;
                    row["Tag"] = dataMember;
                    data.Rows.Add(row);
                    //DevExpress.XtraTreeList.Nodes.TreeListNode node = this.TreeList2.Nodes.Add();
                    //node.Tag = dataMember.Name;
                }
            }
        }

        private void TreeList2_DoubleClick(object sender, EventArgs e)
        {
            if (this.TreeList2.FocusedNode != null)
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        private void TreeList1_CustomDrawNodeCell(object sender, DevExpress.XtraTreeList.CustomDrawNodeCellEventArgs e)
        {
            if (e.Node.Tag is System.ComponentModel.Component component)
                e.CellText = component.Site.Name;
            else
                e.CellText = e.Node.Tag?.ToString();
        }

        private void TreeList2_CustomDrawNodeCell(object sender, DevExpress.XtraTreeList.CustomDrawNodeCellEventArgs e)
        {
            if (e.Node.Tag != null && e.Node.Tag is BindingFieldInfo info)
            {
                e.CellText = info.Caption ?? info.Name;
            }
        }
    }
}
